package com.ht.pms.core.job;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ht.pms.core.dto.JobTimeControl;
import com.ht.pms.core.service.JobTimeControlService;
import com.ht.ussp.util.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Auther: 喻尊龙
 * @Date: 2018/11/27 10:18
 * @Description:
 */
@JobHandler(value = "DateCutHandler")
@Component
public class DateCut extends IJobHandler {

    private static Logger LOG = LoggerFactory.getLogger(DateCut.class);


    @Autowired
    private JobTimeControlService jobTimeControlService;

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        LOG.info("日切开始=====");
        JobTimeControl jobTimeControl = jobTimeControlService.selectOne(new EntityWrapper<JobTimeControl>().eq("id","1"));

        Date last = jobTimeControl.getDateNow();
        Date next = DateUtil.addDay2Date(1,DateUtil.getDate(DateUtil.formatDate(last)));

        String dateNext = DateUtil.formatDate(next);

        jobTimeControl.setDateNow(next);
        jobTimeControl.setDateStart(DateUtil.getDateTime(dateNext+ " 00:00:00"));
        jobTimeControl.setDateEnd(DateUtil.getDateTime(dateNext+" 23:59:59"));

        jobTimeControlService.updateById(jobTimeControl);
        LOG.info("日切结束=====");

        return SUCCESS;
    }

}
